<template>
  <div class="websocket-demo">
    <h1>测试websocket</h1>
    <!--    {{ data }}-->
    <button @click="sendData">发送数据</button>
  </div>
</template>

<script>
import SocketService from "../utils/websocket";
import { inject, reactive } from "vue";

export default {
  name: "demo-websocket",
  setup() {
    //获取公共信息
    let info = inject("info");
    const data = reactive({
      socketServe: SocketService.Instance,
    });
    SocketService.Instance.connect();
    data.socketServe = SocketService.Instance;
    const testData = (data) => {
      console.log(data);
    };
    let params = {
      user_id: info.id,
      check: 0,
      value: "我发送数据了",
    };
    const sendData = () => {
      data.socketServe.send(params);
      console.log(params, "给服务端发送数据");
    };
    data.socketServe.registerCallBack("callback", testData);
    return {
      sendData,
      data,
    };
  },
};
</script>

<style scoped lang="scss">
.websocket-demo {
  width: 300px;
  height: 300px;
  background: rebeccapurple;
}
</style>
